package com.lap.permission.convert;

import com.lap.framework.dto.resource.ClassDTO;
import com.lap.framework.tools.BooleanStrategy;
import com.lap.framework.tools.IntStrategy;
import com.lap.permission.domain.entity.*;
import com.lap.permission.domain.pojo.UserState;
import com.lap.permission.dto.request.*;
import com.lap.permission.dto.response.*;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;

/**
 * 对象类型转换
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
@Mapper(imports = {BooleanStrategy.class, IntStrategy.class}, componentModel = "spring")
public interface Convert {

    //部门转换
    @Mapping(expression = "java(IntStrategy.toString(request.getPidPaths()))", target = "pidPath")
    @Mapping(expression = "java(IntStrategy.getLast(request.getPidPaths()))", target = "pid")
    Department toDepartment(DepartmentRequest request);

    @Mapping(expression = "java(IntStrategy.toList(depart.getPidPath()))", target = "pidPaths")
    DepartmentDTO toDepartDto(Department depart);

    //字典转换
    Dict toDict(DictRequest request);

    DictDetail toDictDetail(DictDetailRequest.DetailRequest detail);

    //菜单转换
    @Mapping(source = "meta.title", target = "title")
    @Mapping(source = "meta.icon", target = "icon")
    @Mapping(expression = "java(IntStrategy.toString(request.getPidPaths()))", target = "pidPath")
    @Mapping(expression = "java(BooleanStrategy.toInteger(request.getMeta().getIsKeepAlive()))", target = "keepAliveFlag")
    @Mapping(expression = "java(BooleanStrategy.toInteger(request.getMeta().getIsAffix()))", target = "affixFlag")
    @Mapping(expression = "java(BooleanStrategy.toInteger(request.getMeta().getIsIframe()))", target = "iframeFlag")
    @Mapping(expression = "java(BooleanStrategy.toInteger(request.getMeta().getIsHide()))", target = "hideFlag")
    Menu toMenu(MenuRequest request);

    @Mapping(target = "children", ignore = true)
    @Mapping(source = "title", target = "meta.title")
    @Mapping(source = "icon", target = "meta.icon")
    @Mapping(expression = "java(IntStrategy.toList(menu.getPidPath()))", target = "pidPaths")
    @Mapping(expression = "java(BooleanStrategy.notBlankToBoolean(menu.getLink()))", target = "meta.isLink")
    @Mapping(expression = "java(BooleanStrategy.toBoolean(menu.getKeepAliveFlag()))", target = "meta.isKeepAlive")
    @Mapping(expression = "java(BooleanStrategy.toBoolean(menu.getAffixFlag()))", target = "meta.isAffix")
    @Mapping(expression = "java(BooleanStrategy.toBoolean(menu.getIframeFlag()))", target = "meta.isIframe")
    @Mapping(expression = "java(BooleanStrategy.toBoolean(menu.getHideFlag()))", target = "meta.isHide")
    MenuDTO toMenuDto(Menu menu);

    //模版转换
    MsgTemplate toMsgTemplate(MsgTemplateRequest request);

    //区域转换

    /**
     * 区域请求转换实体转换
     *
     * @param request 区域请求
     * @return 区域实体
     */
    @Mapping(expression = "java(IntStrategy.toString(request.getPidPaths()))", target = "pidPath")
    @Mapping(expression = "java(IntStrategy.getLast(request.getPidPaths()))", target = "pid")
    Region toRegion(RegionRequest request);

    /**
     * 区域实体转换数据传输
     *
     * @param region 实体
     * @return 区域传输对象
     */
    @Mapping(target = "children", ignore = true)
    @Mapping(expression = "java(IntStrategy.toList(region.getPidPath()))", target = "pidPaths")
    RegionDTO toRegionDto(Region region);

    /**
     * 区域集合转换
     *
     * @param regionList 区域集合
     * @return 区域数据传输集合
     * @see #toRegionDto(Region)
     */
    List<RegionDTO> toRegionDto(List<Region> regionList);

    //角色转换
    Role toRole(RoleRequest request);

    List<RoleDTO> toRoleDto(List<Role> roleList);

    //用户转换
    @Mapping(expression = "java(IntStrategy.toString(request.getDepartmentPaths()))", target = "departmentPath")
    @Mapping(expression = "java(IntStrategy.getLast(request.getDepartmentPaths()))", target = "departmentId")
    User toUser(UserRequest request);

    @Mapping(expression = "java(IntStrategy.toList(user.getDepartmentPath()))", target = "departmentPaths")
    UserDTO toUserDto(User user);

    UserState toUserState(UserStateRequest request);

    //其他
    Resource toResource(ResourceRequest request);

    Resource toResource(ClassDTO dto);

    ServiceLog toServiceLog(ServiceLogRequest request);

    Position toPosition(PositionRequest request);

    Project toProject(ProjectRequest request);

    AccessLog toAccessLog(AccessLogRequest request);

}
